﻿using System.Collections.Generic;

using G.Model;
using G.Runtime;
using GameControls;

namespace GameRuntimeControls
{
    public class GameDisplayerPT : GameDisplayer
    {
        private readonly GameSpace _Game;
        //private readonly ButtonPanel _ButtonPanel;
        //private readonly MessagePanel _MessagePanel;

        public GameDisplayerPT(GameSpace game,
                               BoardShapeContainer container,
                               int pSquareSize
                               // ButtonPanel pButtonPanel,
                               // MessagePanel pMessagePanel
                              )
            : base(game.Def, container, pSquareSize)
        {
            _Game = game;
            // _ButtonPanel = pButtonPanel;
            // _MessagePanel = pMessagePanel;
            game.Controller.ElementsReadyToMoveChangedEvent += this.OnReadyToMoveChanged;
            game.Elements.Actions.ActionsChangedEvent += this.OnActionChanged;
            game.Elements.PlayerChangedEvent += OnPlayerChangedEvent;
            game.Elements.PropertyChangedEvent += OnPropertyChangedEvent;
            game.Elements.ElementAddedEvent += OnElementAddedEvent;
            game.Globals.TopImages.Changed += OnGameLocationChanged;
            Init();
            DisplayReadyToMoveFlag();
        }
        
        public override void Dispose()
        {
            base.Dispose();
            _Game.Controller.ElementsReadyToMoveChangedEvent -= this.OnReadyToMoveChanged;
            _Game.Elements.Actions.ActionsChangedEvent -= this.OnActionChanged;
            _Game.Elements.PlayerChangedEvent -= OnPlayerChangedEvent;
            _Game.Elements.PropertyChangedEvent -= OnPropertyChangedEvent;
            _Game.Elements.ElementAddedEvent -= OnElementAddedEvent;
            _Game.Globals.TopImages.Changed -= OnGameLocationChanged;
        }
        
        public GameSpace Game
        {
            get { return _Game; }
        }
        
        //        public ButtonPanel ButtonPanel
        //        {
        //            get { return _ButtonPanel; }
        //        }
//
        //        public MessagePanel MessagePanel
        //        {
        //            get { return _MessagePanel; }
        //        }
        
        private void OnGameLocationChanged(GameLocation location)
        {
            DisplayIcon(location);
        }
        
        private void OnElementAddedEvent(GameElement subject)
        {
            DisplayIcon(subject.Location);
        }

        private void OnPropertyChangedEvent(GameElement subject)
        {
            DisplayIcon(subject.Location);
        }
        
        private void OnPlayerChangedEvent(GameElement subject)
        {
            DisplayIcon(subject.Location);
        }

        void DisplayIcon(GameLocation location)
        {
            foreach (BoardShapeDisplayer displayer in Displayers) 
            {
                if (displayer.Location == location) 
                {
                    displayer.DisplayIcon();
                    break;
                }
            }
        }

        private void OnReadyToMoveChanged(ICollection<GameElement> elements)
        {
            DisplayReadyToMoveFlag();
        }

        private void DisplayReadyToMoveFlag()
        {

            foreach (BoardShape shape in base.Shapes.Values)
            {
                shape.UpperLeftCorner = false;
            }

            foreach (GameElement e in _Game.Controller.ElementsReadyToMove)
            {
                BoardShape shape = base.Shapes[e.Location];
                shape.UpperLeftCorner = true;
            }
        }

        private void OnActionChanged()
        {
            foreach (GameLocation location in base.Shapes.Keys)
            {
                BoardShape shape = Shapes[location];

                if (_Game.Elements.Actions.Contains(location))
                {
                    shape.DisplayCircle = true;
                }
                else
                {
                    shape.DisplayCircle = false;
                }
            }
        }

        protected override BoardShapeDisplayer CreateDisplayer(
            BoardShape shape)
        {
            return new BoardShapeDisplayerPT(
                this, shape, base.ShapeContainer, _Game);
        }
        
    }
}
